package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.mapper.AddressBookMapper;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Yan
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

    @Override
    public Result<AddressBook> saveAddressBook(AddressBook addressBook) {
        if (ObjectUtils.isEmpty(addressBook)){
            return Result.error("数据错误");
        }

        addressBook.setUserId(BaseContext.getCurrentId());

        this.save(addressBook);

        return Result.success(addressBook);
    }

    @Override
    public Result<AddressBook> setDefault(AddressBook addressBook) {
        if (ObjectUtils.isEmpty(addressBook)){
            return Result.error("数据错误");
        }

        LambdaUpdateWrapper<AddressBook> wrapper =new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        wrapper.set(AddressBook::getIsDefault,0);

        this.update(wrapper);

        addressBook.setIsDefault(1);
        this.updateById(addressBook);

        return Result.success(addressBook);

    }

    @Override
    public Result<AddressBook> findAddressBookById(Long id) {
        if (ObjectUtils.isEmpty(id)){
            return Result.error("数据错误");
        }

        AddressBook byId = this.getById(id);

        if (ObjectUtils.isEmpty(byId)){
            return Result.error("没找到对象");
        }

        return Result.success(byId);
    }

    /**
     * 查询默认地址
     * @return
     */
    @Override
    public Result<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> wrapper =new LambdaQueryWrapper<>();

        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        wrapper.eq(AddressBook::getIsDefault,1);

        AddressBook one = this.getOne(wrapper);

        if (ObjectUtils.isEmpty(one)){
            return Result.error("没找到对象");
        }

        return Result.success(one);
    }

    @Override
    public Result<List<AddressBook>> findAddressBookList(AddressBook addressBook) {
        if (ObjectUtils.isEmpty(addressBook)){
            return Result.error("数据错误");
        }

        addressBook.setUserId(BaseContext.getCurrentId());

        LambdaQueryWrapper<AddressBook> wrapper =new LambdaQueryWrapper<>();

        wrapper.eq(ObjectUtils.isNotEmpty(addressBook.getUserId()),AddressBook::getUserId,addressBook.getUserId());
        wrapper.orderByDesc(AddressBook::getUpdateTime);

        List<AddressBook> list = this.list(wrapper);

        return Result.success(list);
    }

    @Override
    public Result<String> updateAddressBook(AddressBook addressBook) {
        if (ObjectUtils.isEmpty(addressBook)){
            return Result.error("参数错误");
        }
        this.updateById(addressBook);

        return Result.success("修改成功");
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @Override
    public Result<String> deleteAddressBook(Long ids) {
        if (ObjectUtils.isEmpty(ids)){
            return Result.error("数据错误");
        }
        this.removeById(ids);

        return Result.success("删除地址成功");
    }
}